package com.example.fuluchou.controller;

import com.example.fuluchou.common.errorcode.ControllerErrorCodeConstants;
import com.example.fuluchou.common.exception.ControllerException;
import com.example.fuluchou.common.pojo.CommonResult;
import com.example.fuluchou.common.untils.JacksonUtil;
import com.example.fuluchou.controller.param.UserPasswordLoginParam;
import com.example.fuluchou.controller.param.UserRegisterParam;
import com.example.fuluchou.controller.param.UserVerificationCodeLoginParam;
import com.example.fuluchou.controller.result.UserBaseInfoResult;
import com.example.fuluchou.controller.result.UserLoginResult;
import com.example.fuluchou.controller.result.UserRegisterResult;
import com.example.fuluchou.service.UserService;
import com.example.fuluchou.service.VerificationCodeService;
import com.example.fuluchou.service.dto.UserFindDTO;
import com.example.fuluchou.service.dto.UserLoginDTO;
import com.example.fuluchou.service.dto.UserRegisterDTO;
import com.example.fuluchou.service.enums.UserIdentityEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
public class UserController {
    @Autowired
    VerificationCodeService verificationCodeService;
    @Autowired
    private UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 注册
     */
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(
            @Validated @RequestBody UserRegisterParam param) {
        //Validated 注解 表示param参数需要验证格式
        //日志打印
        logger.info("userRegister UserRegisterParam:{}", JacksonUtil.writeValueAsString(param));

        UserRegisterDTO userRegisterDTO = userService.register(param);
        return CommonResult.success(convertToUserRegisterRsult(userRegisterDTO));

    }

    /**
     * 发送验证码
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendCode(@RequestParam(value = "phoneNumber",required = false)
                                              String phoneNumber){
        logger.info("sendVerificationCode phoneNumber: " + phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     * @param param
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> UserPasswordLogin(
            @Validated @RequestBody UserPasswordLoginParam param) {
        logger.info("UserPasswordLogin UserPasswordLoginParam:{}"
                    ,JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }



    /**
     * 验证码登录
     * @param param
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> UserVerificationCodeLogin(
            @Validated @RequestBody UserVerificationCodeLoginParam param) {
        logger.info("UserPasswordLogin UserPasswordLoginParam:{}"
                ,JacksonUtil.writeValueAsString(param));
        UserLoginDTO userLoginDTO = userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    /**
     * 查询人员列表
     *
     * @return
     */
    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserBaseInfoResult>> findUserBaseInfoList(
            @RequestParam(value = "identity",required = false)String identity) {
        logger.info("findUserBaseInfoList identity:{}",identity);
        List<UserFindDTO> userFindDTOList = userService.findUserBaseInfoList(UserIdentityEnum.forName(identity));
        return CommonResult.success(convertToUserBaseInfo(userFindDTOList));
    }

    private List<UserBaseInfoResult> convertToUserBaseInfo(List<UserFindDTO> userFindDTOList) {
        if(CollectionUtils.isEmpty(userFindDTOList)){
            return Arrays.asList();
        }
        return userFindDTOList.stream()
                .map(userFindDTO->{
                    UserBaseInfoResult result = new UserBaseInfoResult();
                    result.setUserId(userFindDTO.getUserId());
                    result.setUserName(userFindDTO.getUserName());
                    result.setIdentity(userFindDTO.getIdentity());
                    return result;
                }).collect(Collectors.toList());
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(null == userLoginDTO){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_EEROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    private UserRegisterResult convertToUserRegisterRsult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult result = new UserRegisterResult();
        if (null == userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }
}




















